package com.restphp.java.utils.file;

import com.restphp.java.utils.string.core.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sofical on 2020/7/31.
 */
public class FileUtils {
    /**
     * 写文件.
     * @param content 内容.
     * @param savePath 保存路径.
     */
    public static void save(String content, String savePath) {
        BufferedWriter writer = null;
        try {
            //目标文件组成
            //检查是否存在，不存在创建.
            File file = new File(savePath);
            if(!file.exists()){
                file.createNewFile();
            }
            //写内容
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            writer.write(content);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != writer) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取文件内容
     * @param filePath 文件路径.
     * @return 文件内容.
     */
    public static String get(String filePath) {
        return get(filePath, "\n", "UTF-8");
    }

    /**
     * 获取文件内容
     * @param filePath 文件路径.
     * @param lineSep 源文件换行符.
     * @param charset 源文件格式.
     * @return 文件内容.
     */
    public static String get(String filePath, String lineSep, String charset) {
        File file = new File(filePath);
        String content = "";
        if(!file.exists()){
            return null;
        }
        BufferedReader reader = null;
        try {
            InputStreamReader inputStreamReader = StringUtils.isBlank(charset) ? new InputStreamReader(new FileInputStream(file)) : new InputStreamReader(new FileInputStream(file), charset);
            reader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = reader.readLine()) != null) {
                content += ("".equals(content) ? "" : lineSep) + line;
            }
        } catch (Exception e) {

        } finally {
            if (null != reader) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return content;
    }

    public static String[] getArr(String filePath, Integer fromLineNumber, Integer endLineNumber, String charset) {
        File file = new File(filePath);
        List<String> content = new ArrayList<>();
        if(!file.exists()){
            return new String[0];
        }
        BufferedReader reader = null;
        try {
            InputStreamReader inputStreamReader = StringUtils.isBlank(charset) ? new InputStreamReader(new FileInputStream(file)) : new InputStreamReader(new FileInputStream(file), charset);
            reader = new BufferedReader(inputStreamReader);
            String line;
            Integer num = 0;
            while ((line = reader.readLine()) != null) {
                if (num >= fromLineNumber && num <endLineNumber) {
                    content.add(line);
                }
            }
        } catch (Exception e) {

        } finally {
            if (null != reader) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return content.toArray(new String[content.size()]);
    }
}
